﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace myClient
{
    public class appIcons
    {
        private enum ICON_ID_LIST
        {
            OTHERS =0,
            MAIN,
            TXT,
            DLL,
            EXE,
            MEDIA,
            PIC,
            ZIP,
        }

        private const int SIZE_NORMAL = 32;
        private const int SIZT_SMALL = 16;
        private const int SIZE_LARGE = 48;

        private string _ResFilePath = Environment.SystemDirectory + "\\shell32.dll";

        private Icon _mainIcon;
        private ImageList _iconLists = new ImageList();
        private ImageList _smallIconList = new ImageList();
        private ImageList _largeIconLists = new ImageList();

        private int[] _iconIndexs = new int[] {
            0,
            164, 
            70, 72, 2, 118, 117, 0
        };

        public ImageList IconList
        {
            get
            {
                return _iconLists;
            }
        }

        public ImageList SmallIconList
        {
            get 
            {
                return _smallIconList;
            }
        }

        public ImageList LargeIconList
        {
            get
            {
                return _largeIconLists;
            }
        }

        public Icon MainIcon
        {
            get
            {
                return _mainIcon;
            }
        }

        public appIcons()
        {
            _iconLists.ColorDepth = ColorDepth.Depth32Bit;
            _smallIconList.ColorDepth = ColorDepth.Depth32Bit;
            _largeIconLists.ColorDepth = ColorDepth.Depth32Bit;

            _iconLists.ImageSize = new Size(SIZE_NORMAL, SIZE_NORMAL);
            _smallIconList.ImageSize = new Size(SIZT_SMALL, SIZT_SMALL);
            _largeIconLists.ImageSize = new Size(SIZE_LARGE, SIZE_LARGE);

            LoadIcons();
        }


        private void LoadIcons()
        {
            try
            {
                IntPtr handle = Dll.Shell32.ExtractIcon(IntPtr.Zero, _ResFilePath, _iconIndexs[(int)ICON_ID_LIST.MAIN]);
                _mainIcon = (Icon)Icon.FromHandle(handle).Clone();
                Dll.User32.DestroyIcon(handle);

                for (int i = 0; i < _iconIndexs.Length; i++)
                {
                    handle = Dll.Shell32.ExtractIcon(IntPtr.Zero, _ResFilePath, _iconIndexs[i]);

                    _iconLists.Images.Add((Icon)Icon.FromHandle(handle).Clone());
                    _smallIconList.Images.Add((Icon)Icon.FromHandle(handle).Clone());
                    _largeIconLists.Images.Add((Icon)Icon.FromHandle(handle).Clone());

                    Dll.User32.DestroyIcon(handle);
                }
            }
            catch (Exception)
            { 
            }
        }

        public int GetIconIndex(string FileExt)
        {
            string ext = FileExt.ToUpper();

            if (ext.Equals(".TXT") || ext.Equals(".INI") || ext.Equals(".LOG"))
                return (int)ICON_ID_LIST.TXT;
            else if (ext.Equals(".DLL"))
                return (int)ICON_ID_LIST.DLL;
            else if (ext.Equals(".EXE") || ext.Equals(".COM"))
                return (int)ICON_ID_LIST.EXE;
            else if (ext.Equals(".AVI") || ext.Equals(".WMV") || ext.Equals(".WMA") || ext.Equals(".RMVB"))
                return (int)ICON_ID_LIST.MEDIA;
            else if (ext.Equals(".JPG") || ext.Equals(".BMP") || ext.Equals(".GIF"))
                return (int)ICON_ID_LIST.PIC;
            else
                return (int)ICON_ID_LIST.OTHERS;

        }
    }
}
